package dyyx;

import java.util.HashMap;
import java.util.Map;

import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.GroovyShell;
import groovy.util.GroovyScriptEngine;

public class GroovyDemo {

	public static void main(String[] args) throws Exception {	
		System.out.println("GroovyDemo");	
		
        String expression = " double x = dyyx.Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";	
		expression = "import dyyx.Utils; double x = Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";	
		// expression = "import dyyx.Utils; if(a>0){a=3;} ; double x = Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";

		// 单引号 不会解析变量 返回 $a 
		// expression = "String str = '$a';return str";
		// 双引号会解析变量  返回 1 
		// expression = "String str = \"$a\"; System.out.println(\"str=\"+str);return str";
		
        Binding context = new Binding();
        context.setProperty("a", 1);
        context.setProperty("b", 2);
        context.setProperty("c", 3);
	
        // 直接执行
        GroovyShell shell = new GroovyShell(context);
        Object result = null;
        result = shell.evaluate(expression);   
        System.out.println(result);
        
        
               
        // 编译执行
        GroovyClassLoader loader = null;	
	    loader = new GroovyClassLoader();
		Class groovyClass = loader.parseClass(expression);	
		// 可以缓存 groovyClass	
		System.out.println(groovyClass);
		GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();	
		// groovyObject 不可缓存	
		groovyObject.setProperty("a", 1);
		groovyObject.setProperty("b", 2);
		// 参数不传会报错  groovy.lang.MissingPropertyException: No such property: c for class: script15628934582201596470415
		groovyObject.setProperty("c", 3);	
		// Caused by: groovy.lang.MissingMethodException: No signature of method: java.lang.String.div() is applicable for argument types: (Integer) values: [3]
		// groovyObject.setProperty("c", "3");
		result = groovyObject.invokeMethod("run", new Object[]{}); 
        System.out.println(result);
        
        
        ClassLoader loadertmp = groovyObject.getClass().getClassLoader();      
        while(loadertmp != null) {
            System.out.println(loadertmp); 
            loadertmp = loadertmp.getParent();
        }      

        
        GroovyScriptEngine gse = null;
        
        //  转换成具体接口
        StringBuilder code = new StringBuilder();
        code.append("package dyyx;").append("\n");
        code.append("import java.time.LocalDateTime;").append("\n");
        code.append("import java.util.Map;").append("\n");
        code.append("public class TestIntfImpl implements dyyx.RunIntf {").append("\n");
        code.append("public Object execute(Map context) {").append("\n");
        // code.append("return LocalDateTime.now().toString();").append("\n");
        code.append("return 'hello,'+context.get('name');").append("\n");
        code.append("}").append("\n");
        code.append("}").append("\n");


        System.out.println(code);

        groovyClass = loader.parseClass(code.toString());	
		System.out.println(groovyClass);
		RunIntf intf = (RunIntf) groovyClass.newInstance();	
        Map map = new HashMap();
        map.put("name", "dyyx");
		System.out.println(intf.execute(map));

        
	}
	
	
		
}
